package com.whmskj.xjlhsc.mobie.empProPlan.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.util.StringUtil;
import com.whmskj.xjlhbc.domain.CorpMember;
import com.whmskj.xjlhbc.domain.Yg01employeefiles;
import com.whmskj.xjlhbc.mapper.CorpMemberMapper;
import com.whmskj.xjlhsc.entity.Sb02EquipmentCounterAccount;
import com.whmskj.xjlhsc.enums.ScEnums;
import com.whmskj.xjlhsc.exception.BusinessException;
import com.whmskj.xjlhsc.jitapi.dto.Emp;
import com.whmskj.xjlhsc.jitapi.entity.Device;
import com.whmskj.xjlhsc.jitapi.mapper.DeviceMapper;
import com.whmskj.xjlhsc.jitapi.mapper.EmpMapper;
import com.whmskj.xjlhsc.mapper.Sb02EquipmentCounterAccountMapper;
import com.whmskj.xjlhsc.mapper.Yg01employeefilesMapper;
import com.whmskj.xjlhsc.mobie.Scheduling.Dto.DcSchedulingQueryDto;
import com.whmskj.xjlhsc.mobie.Scheduling.Dto.DevicePlatformChangeDto;
import com.whmskj.xjlhsc.mobie.Scheduling.Dto.GetProductDeviceDto;
import com.whmskj.xjlhsc.mobie.Scheduling.Dto.UpdatePersonExchangeDto;
import com.whmskj.xjlhsc.mobie.Scheduling.entity.vo.DcSchedulingVo;
import com.whmskj.xjlhsc.mobie.empProPlan.dto.ScanDeviceDto;
import com.whmskj.xjlhsc.mobie.empProPlan.entity.ProPersonnelStandConfirm;
import com.whmskj.xjlhsc.mobie.empProPlan.mapper.ProPersonnelStandConfirmMapper;
import com.whmskj.xjlhsc.mobie.empProPlan.service.ProPersonnelStandConfirmService;
import com.whmskj.xjlhsc.utils.SCUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.format.DateTimeFormatters;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author lk
 * @since 2025-07-07 11:01:25
 */
@Service
@Slf4j
public class ProPersonnelStandConfirmServiceImpl extends ServiceImpl<ProPersonnelStandConfirmMapper, ProPersonnelStandConfirm> implements ProPersonnelStandConfirmService {

    @Autowired
    private ProPersonnelStandConfirmMapper proPersonnelStandConfirmMapper;
    @Autowired
    private Sb02EquipmentCounterAccountMapper sb02EquipmentCounterAccountMapper;

    @Override
    public Page<ProPersonnelStandConfirm> findPage(ProPersonnelStandConfirm params) {
        Page<ProPersonnelStandConfirm> page = new Page<>(1, 10);//TODO 自行处理
        LambdaQueryWrapper<ProPersonnelStandConfirm> query = Wrappers.lambdaQuery(ProPersonnelStandConfirm.class);
        return proPersonnelStandConfirmMapper.selectPage(page, query);
    }

    @Override
    public List<ProPersonnelStandConfirm> findList(ProPersonnelStandConfirm params) {
        LambdaQueryWrapper<ProPersonnelStandConfirm> query = Wrappers.lambdaQuery(ProPersonnelStandConfirm.class);
        return proPersonnelStandConfirmMapper.selectList(query);
    }

    @Override
    public ProPersonnelStandConfirm findById(Long id) {
        return proPersonnelStandConfirmMapper.selectById(id);
    }

    @Override
    public boolean insert(ProPersonnelStandConfirm proPersonnelStandConfirm) {
        return save(proPersonnelStandConfirm);
    }

    @Override
    public boolean update(ProPersonnelStandConfirm proPersonnelStandConfirm) {
        return updateById(proPersonnelStandConfirm);
    }

    @Override
    public int delete(Long id) {
        return proPersonnelStandConfirmMapper.deleteById(id);
    }


    @Autowired
    private EmpMapper empMapper;
    @Override
    public List<DcSchedulingVo> DcSchedulingList(DcSchedulingQueryDto queryDto) {
        if (queryDto.getTypeOfWork().equals("全部")) {
            queryDto.setTypeOfWork(null);
        }

        ArrayList<DcSchedulingVo> dcSchedulingVos = new ArrayList<>();

        List<DcSchedulingVo> list = proPersonnelStandConfirmMapper.selectDcSchedulingList(queryDto,0);
        Emp currentLoginEmp = SCUtil.getCurrentLoginEmp();

        List<Emp> emps = empMapper.empListByClassName(currentLoginEmp.getEmpName());

        Map<Integer, List<DcSchedulingVo>> collect = list.stream().collect(Collectors.groupingBy(DcSchedulingVo::getStaffId));

        Set<Integer> keys = collect.keySet();
        for (Integer key : keys) {
            List<DcSchedulingVo> list1 = collect.get(key);
            DcSchedulingVo dcSchedulingVo1 = list1.get(0);

            String theHead = dcSchedulingVo1.getTheHead();
            if (StringUtil.isNotEmpty(theHead)) {
                int startIndex = theHead.indexOf("\"url\":") + 8;
                int endIndex = theHead.indexOf(",", startIndex) - 1;
                String substring = theHead.substring(startIndex, endIndex);
                dcSchedulingVo1.setTheHead(substring);
            }
            List<String> machineryList = dcSchedulingVo1.getMachineryList();
            for (DcSchedulingVo schedulingVo : list1) {
                String machineryObj = schedulingVo.getMachineryObj();
                if (StringUtil.isNotEmpty(machineryObj)) {
                    Collection<ScanDeviceDto> scanDeviceDtos = parseDeviceIds(machineryObj);
                    List<String> list2 = scanDeviceDtos.stream().map(ScanDeviceDto::getDeviceCode).toList();
                    machineryList.addAll(list2);
                }
            }
            dcSchedulingVo1.setMachineryList(machineryList);
            dcSchedulingVo1.setMachineryObj(null);
            dcSchedulingVos.add(dcSchedulingVo1);
        }

        Map<Integer, List<DcSchedulingVo>> collect1 = dcSchedulingVos.stream().collect(Collectors.groupingBy(DcSchedulingVo::getStaffId));
        for (Emp emp : emps) {
            List<DcSchedulingVo> dcSchedulingVos1 = collect1.get(emp.getEmpId());
            if (dcSchedulingVos1 != null){
                dcSchedulingVos1.get(0).setCanOperation(true);
            }
        }

        for (DcSchedulingVo dcSchedulingVo : dcSchedulingVos) {
            List<String> machineryList = dcSchedulingVo.getMachineryList();
            for (String platform : machineryList) {
                platform.replace("#","");
            }
        }

        return dcSchedulingVos;
    }

    @Override
    public List<Map<String, Object>> getSchedulingDate(String process, String startTime, String endTime) {
        return proPersonnelStandConfirmMapper.selectSchedulingDate(process, startTime, endTime);
    }

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public List<ScanDeviceDto> getProductDevice(GetProductDeviceDto getProductDeviceDto) {

        LambdaQueryWrapper<ProPersonnelStandConfirm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProPersonnelStandConfirm::getStaffId, getProductDeviceDto.getStaffId()).eq(ProPersonnelStandConfirm::getClasses, getProductDeviceDto.getShift()).eq(ProPersonnelStandConfirm::getProcess, getProductDeviceDto.getProcess()).in(ProPersonnelStandConfirm::getConfirmStatus, ScEnums.ConfirmStatus.CONFIRMED.name(), ScEnums.ConfirmStatus.UNCONFIRMED.name())
                .last("and DATE(date_ymd)=DATE('" + getProductDeviceDto.getDate() + "')");
        List<ProPersonnelStandConfirm> confirmList = list(queryWrapper);
        List<ScanDeviceDto> currentEmpConfirmDeviceIds = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(confirmList)) {
            confirmList.forEach(cur -> currentEmpConfirmDeviceIds.addAll(parseDeviceIds(cur.getMachineryObj())));
        }
        Map<String, ScanDeviceDto> currentConfirmG = currentEmpConfirmDeviceIds.stream().collect(Collectors.toMap(cur -> cur.getDeviceId() + cur.getAbTag(), Function.identity()));
        LambdaQueryWrapper<ProPersonnelStandConfirm> proPersonnelStandConfirmLambdaQueryWrapper = new LambdaQueryWrapper<>();
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getProcess, getProductDeviceDto.getProcess())
                .eq(ProPersonnelStandConfirm::getClasses, getProductDeviceDto.getShift())
                .in(ProPersonnelStandConfirm::getConfirmStatus, ScEnums.ConfirmStatus.CONFIRMED.name(), ScEnums.ConfirmStatus.UNCONFIRMED.name())
                .last("and DATE(date_ymd)=DATE('" + getProductDeviceDto.getDate() + "')");

        List<Device> devices = deviceMapper.selectByProcess(getProductDeviceDto.getProcess());
        List<ScanDeviceDto> scanDeviceDtoList = new ArrayList<>(devices.stream().map(this::getScanDeviceDto).toList());

        if ("细纱".equals(getProductDeviceDto.getProcess())) {
            //细纱区分A/B
            List<ScanDeviceDto> bList = BeanUtil.copyToList(scanDeviceDtoList, ScanDeviceDto.class);
            scanDeviceDtoList.forEach(cur -> cur.setAbTag("A"));
            bList.forEach(cur -> cur.setAbTag("B"));
            scanDeviceDtoList.addAll(bList);

        }
        //查询当天已被选择的设备
        LambdaQueryWrapper<ProPersonnelStandConfirm> qe = new LambdaQueryWrapper<>();
        qe.eq(ProPersonnelStandConfirm::getClasses, getProductDeviceDto.getShift())
                .in(ProPersonnelStandConfirm::getConfirmStatus, ScEnums.ConfirmStatus.CONFIRMED.name(), ScEnums.ConfirmStatus.UNCONFIRMED.name())
                .last("and DATE(date_ymd)=DATE('" + getProductDeviceDto.getDate() + "')");
        List<ProPersonnelStandConfirm> confirmListMains = list(qe);

        List<ScanDeviceDto> allConfirmDeviceIds = new ArrayList<>();
        confirmListMains.forEach(cur -> allConfirmDeviceIds.addAll(parseDeviceIds(cur.getMachineryObj())));
        Map<String, ScanDeviceDto> allConfirmG = allConfirmDeviceIds.stream().collect(Collectors.toMap(cur -> cur.getDeviceId() + cur.getAbTag(), Function.identity()));

        for (ScanDeviceDto deviceDto : scanDeviceDtoList) {
            if (ObjectUtil.isNotNull(allConfirmG.get(deviceDto.getDeviceId() + deviceDto.getAbTag()))) {
                deviceDto.setDeviceSignState("Y");
                if (ObjectUtil.isNotNull(currentConfirmG.get(deviceDto.getDeviceId() + deviceDto.getAbTag())))
                    deviceDto.setCurrentFlag("Y");
            }
        }


            int count = 1;
            for (ScanDeviceDto scanDeviceDto : scanDeviceDtoList) {
                scanDeviceDto.setBusinessId(count++);
                scanDeviceDto.setDeviceCode(scanDeviceDto.getDeviceCode().replace("#",""));
            }

        return scanDeviceDtoList;
    }


    @Autowired
    private CorpMemberMapper corpMemberMapper;
    @Autowired
    private Yg01employeefilesMapper yg01employeefilesMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDevicePlatChange(DevicePlatformChangeDto devicePlatformChangeDto) {
          //TODO:验证当前登录用户的权限
        Emp currentLoginEmp = SCUtil.getCurrentLoginEmp();
        LambdaQueryWrapper<ProPersonnelStandConfirm> proPersonnelStandConfirmLambdaQueryWrapper = new LambdaQueryWrapper<>();
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getId,devicePlatformChangeDto.getId());
        ProPersonnelStandConfirm confirm1 = proPersonnelStandConfirmMapper.selectOne(proPersonnelStandConfirmLambdaQueryWrapper);
        LambdaQueryWrapper<Yg01employeefiles> yg01employeefilesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yg01employeefilesLambdaQueryWrapper.eq(Yg01employeefiles::getId,confirm1.getStaffId());
        Yg01employeefiles yg01employeefiles = yg01employeefilesMapper.selectOne(yg01employeefilesLambdaQueryWrapper);
        if (!yg01employeefiles.getClasses().contains(currentLoginEmp.getEmpName())){
            throw new RuntimeException("没有权限");
        }
        ProPersonnelStandConfirm confirm = new ProPersonnelStandConfirm();
        CopyOptions copyOptions = new CopyOptions();
        copyOptions.setIgnoreProperties("id");
        BeanUtil.copyProperties(confirm1,confirm,copyOptions);

        proPersonnelStandConfirmLambdaQueryWrapper = new LambdaQueryWrapper<>();
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getClasses,devicePlatformChangeDto.getShift())
                .eq(ProPersonnelStandConfirm::getProcess,devicePlatformChangeDto.getProcess())
                .eq(ProPersonnelStandConfirm::getStaffId,confirm1.getStaffId())
                .last("and DATE(date_ymd)=DATE('" + devicePlatformChangeDto.getDate() + "')");
        remove(proPersonnelStandConfirmLambdaQueryWrapper);

        List<ScanDeviceDto> scanDeviceDtoList = devicePlatformChangeDto.getScanDeviceDtoList();

        SCUtil.ClassTimeRange range = SCUtil.getClassDateTimeRange();
        String classes = range.getScclass().getDescription();
        synchronized (this) {

            //查看有没有重复签到机器的
            LambdaQueryWrapper<ProPersonnelStandConfirm> queryWrapper = new LambdaQueryWrapper<>();
            //这个时间，这个班次，这个工序所有打卡和未打卡的机器
            queryWrapper.eq(ProPersonnelStandConfirm::getClasses, devicePlatformChangeDto.getShift()).eq(ProPersonnelStandConfirm::getProcess, devicePlatformChangeDto.getProcess()).in(ProPersonnelStandConfirm::getConfirmStatus, ScEnums.ConfirmStatus.CONFIRMED.getDescription(), ScEnums.ConfirmStatus.UNCONFIRMED.getDescription()).last("and DATE(date_ymd)=DATE('" + devicePlatformChangeDto.getDate() + "') and id !="+devicePlatformChangeDto.getId());
            List<ProPersonnelStandConfirm> confirmList = list(queryWrapper);
            if (CollectionUtil.isNotEmpty(confirmList)) {//如果有
                Set<ScanDeviceDto> deviceDtos = new HashSet<>();
                //遍历考勤列表，获得已经被打卡的机器
                confirmList.forEach(cur -> deviceDtos.addAll(parseDeviceIds(cur.getMachineryObj())));

                Map<String, ScanDeviceDto> collect = deviceDtos.stream().collect(Collectors.toMap(cur -> cur.getDeviceId() + cur.getAbTag(), Function.identity()));
                for (ScanDeviceDto deviceDto : scanDeviceDtoList) {
                    if (ObjectUtil.isNotNull(collect.get(deviceDto.getDeviceId() + deviceDto.getAbTag()))) {
                        throw new BusinessException(deviceDto.getDeviceName() + deviceDto.getDeviceCode() + deviceDto.getAbTag() + "已被绑定");
                    }
                }
            }
        }
        String collect = scanDeviceDtoList.stream().map(ScanDeviceDto::getDeviceCode).collect(Collectors.joining(","));
        confirm.setWorkstation(collect);
        try {
            confirm.setMachineryObj(json.writeValueAsString(scanDeviceDtoList));
        } catch (JsonProcessingException e) {
            log.error(e.getMessage());
            throw new BusinessException("解析机台异常");
        }

        save(confirm);

    }

    @Override
    public void deleteConfirm(Long id) {
        //TODO:验证当前登录用户的权限
        Emp currentLoginEmp = SCUtil.getCurrentLoginEmp();
        LambdaQueryWrapper<ProPersonnelStandConfirm> proPersonnelStandConfirmLambdaQueryWrapper = new LambdaQueryWrapper<>();
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getId,id);
        ProPersonnelStandConfirm confirm1 = proPersonnelStandConfirmMapper.selectOne(proPersonnelStandConfirmLambdaQueryWrapper);
        LambdaQueryWrapper<Yg01employeefiles> yg01employeefilesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yg01employeefilesLambdaQueryWrapper.eq(Yg01employeefiles::getId,confirm1.getStaffId());
        Yg01employeefiles yg01employeefiles = yg01employeefilesMapper.selectOne(yg01employeefilesLambdaQueryWrapper);
        if (!yg01employeefiles.getClasses().contains(currentLoginEmp.getEmpName())){
            throw new RuntimeException("没有权限");
        }
        proPersonnelStandConfirmLambdaQueryWrapper = new LambdaQueryWrapper<>();
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getStaffId,confirm1.getStaffId());//这个人
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getClasses,confirm1.getClasses());//这个班次
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getDateYmd,confirm1.getDateYmd());//这一天
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getProcess,confirm1.getProcess());//这个工序
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getConfirmStatus,ScEnums.ConfirmStatus.CONFIRMED.name());
        remove(proPersonnelStandConfirmLambdaQueryWrapper);
    }

    @Override
    public List<Yg01employeefiles> getPersonExchangeList(String keyWord) {
        Emp currentLoginEmp = SCUtil.getCurrentLoginEmp();
        LambdaQueryWrapper<Yg01employeefiles> yg01employeefilesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtil.isNotEmpty(keyWord)){
            yg01employeefilesLambdaQueryWrapper.last("where classes like '%"+currentLoginEmp.getEmpName()+"%' and name like '%"+keyWord+"%' and productionOfStaff ='是'");
        }
        else {
            yg01employeefilesLambdaQueryWrapper.last("where classes like '%" + currentLoginEmp.getEmpName() + "%' and productionOfStaff ='是'");
        }
        List<Yg01employeefiles> yg01employeefilesList = yg01employeefilesMapper.selectList(yg01employeefilesLambdaQueryWrapper);
        if (CollectionUtil.isNotEmpty(yg01employeefilesList)){
            for (Yg01employeefiles yg01employeefiles : yg01employeefilesList) {
                String theHead = yg01employeefiles.getTheHead();
                if (StringUtil.isNotEmpty(theHead)) {
                    Integer startIndex = theHead.indexOf("\"url\":") + 8;
                    Integer endIndex = theHead.indexOf("\"", startIndex);
                    String substring = theHead.substring(startIndex, endIndex);
                    yg01employeefiles.setTheHead(substring);
                }
            }
        }


        return yg01employeefilesList;
    }

    @Override
    public void updatePersonExchange(UpdatePersonExchangeDto updatePersonExchangeDto) {
        //TODO:验证当前登录用户的权限
        Emp currentLoginEmp = SCUtil.getCurrentLoginEmp();
        LambdaQueryWrapper<ProPersonnelStandConfirm> proPersonnelStandConfirmLambdaQueryWrapper = new LambdaQueryWrapper<>();
        proPersonnelStandConfirmLambdaQueryWrapper.eq(ProPersonnelStandConfirm::getId,updatePersonExchangeDto.getId());
        proPersonnelStandConfirmLambdaQueryWrapper.select(ProPersonnelStandConfirm::getStaffId);
        ProPersonnelStandConfirm confirm1 = proPersonnelStandConfirmMapper.selectOne(proPersonnelStandConfirmLambdaQueryWrapper);
        LambdaQueryWrapper<Yg01employeefiles> yg01employeefilesLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yg01employeefilesLambdaQueryWrapper.eq(Yg01employeefiles::getId,confirm1.getStaffId());
        Yg01employeefiles yg01employeefiles = yg01employeefilesMapper.selectOne(yg01employeefilesLambdaQueryWrapper);
        if (!yg01employeefiles.getClasses().contains(currentLoginEmp.getEmpName())){
            throw new RuntimeException("没有权限");
        }
        LambdaUpdateWrapper<ProPersonnelStandConfirm> proPersonnelStandConfirmLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        proPersonnelStandConfirmLambdaUpdateWrapper.set(ProPersonnelStandConfirm::getStaffId,updatePersonExchangeDto.getNewEmpId())
                .eq(ProPersonnelStandConfirm::getProcess,updatePersonExchangeDto.getProcess())
                .eq(ProPersonnelStandConfirm::getStaffId,updatePersonExchangeDto.getPreEmpId())
                .eq(ProPersonnelStandConfirm::getClasses,updatePersonExchangeDto.getShift())
                .apply("DATE(date_ymd) = {0}", updatePersonExchangeDto.getDate());
        update(proPersonnelStandConfirmLambdaUpdateWrapper);
    }
    @Override
    public List<DcSchedulingVo> personSchedulingList(DcSchedulingQueryDto dcSchedulingQueryDto) {
        if ("全部".equals(dcSchedulingQueryDto.getTypeOfWork())){
            dcSchedulingQueryDto.setTypeOfWork(null);
        }
        List<DcSchedulingVo> dcSchedulingVos = proPersonnelStandConfirmMapper.selectDcSchedulingList(dcSchedulingQueryDto, 1);
        List<DcSchedulingVo> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dcSchedulingVos)) {
            Emp currentLoginEmp = SCUtil.getCurrentLoginEmp();

            Map<Integer, List<DcSchedulingVo>> collect = dcSchedulingVos.stream().collect(Collectors.groupingBy(DcSchedulingVo::getStaffId));
            Set<Integer> keySet = collect.keySet();
            for (Integer key : keySet) {
                DcSchedulingVo dcSchedulingVo = collect.get(key).get(0);
                String theHead = dcSchedulingVo.getTheHead();
                if (StringUtil.isNotEmpty(theHead)) {
                    Integer startIndex = theHead.indexOf("\"url\":") + 8;
                    Integer endIndex = theHead.indexOf("\"", startIndex);
                    String substring = theHead.substring(startIndex, endIndex);
                    dcSchedulingVo.setTheHead(substring);
                }
                resultList.add(dcSchedulingVo);
            }

            List<Emp> emps = empMapper.empListByClassName(currentLoginEmp.getEmpName());
            Map<Integer, List<Emp>> collect1 = emps.stream().collect(Collectors.groupingBy(Emp::getEmpId));

            for (DcSchedulingVo dcSchedulingVo : resultList) {
                List<Emp> emps1 = collect1.get(dcSchedulingVo.getStaffId());
                if (CollectionUtil.isNotEmpty(emps1)){
                    dcSchedulingVo.setCanOperation(true);
                }
            }
        }
        return resultList;
    }

    @Override
    public List<List<ScanDeviceDto>> getProductDeviceGroup(GetProductDeviceDto getProductDeviceDto) {
        List<ScanDeviceDto> productDevice = getProductDevice(getProductDeviceDto);
        List<List<ScanDeviceDto>> result=new ArrayList<>();
        if (getProductDeviceDto.getProcess().equals("细纱")) {
            List<ScanDeviceDto> one = productDevice.stream().filter(item ->Integer.parseInt(item.getDeviceCode().replace("#",""))  <= 21).collect(Collectors.toList());
            List<ScanDeviceDto> two = productDevice.stream().filter(item -> Integer.parseInt(item.getDeviceCode().replace("#","")) >= 22 && Integer.parseInt(item.getDeviceCode().replace("#","")) <= 42).collect(Collectors.toList());
            List<ScanDeviceDto> three = productDevice.stream().filter(item -> Integer.parseInt(item.getDeviceCode().replace("#",""))>= 43 && Integer.parseInt(item.getDeviceCode().replace("#","")) <= 63).collect(Collectors.toList());
            List<ScanDeviceDto> four = productDevice.stream().filter(item -> Integer.parseInt(item.getDeviceCode().replace("#","")) >= 64 && Integer.parseInt(item.getDeviceCode().replace("#","")) <= 84).collect(Collectors.toList());
            result.add(one);
            result.add(two);
            result.add(three);
            result.add(four);
        } else if (getProductDeviceDto.getProcess().equals("梳棉")) {
            List<ScanDeviceDto> one = productDevice.stream().filter(item -> Integer.parseInt(item.getDeviceCode().replace("#","")) <= 18).collect(Collectors.toList());
            List<ScanDeviceDto> two = productDevice.stream().filter(item -> Integer.parseInt(item.getDeviceCode().replace("#","")) >= 19 && Integer.parseInt(item.getDeviceCode().replace("#","")) <= 33).collect(Collectors.toList());
            List<ScanDeviceDto> three = productDevice.stream().filter(item ->Integer.parseInt(item.getDeviceCode().replace("#",""))>= 34 && Integer.parseInt(item.getDeviceCode().replace("#","")) <= 52).collect(Collectors.toList());
            result.add(one);
            result.add(two);
            result.add(three);
        } else if (getProductDeviceDto.getProcess().equals("络筒")) {
            List<ScanDeviceDto> one = productDevice.stream().filter(item -> Integer.parseInt(item.getDeviceCode().replace("#","")) <= 14).collect(Collectors.toList());
            List<ScanDeviceDto> two = productDevice.stream().filter(item -> Integer.parseInt(item.getDeviceCode().replace("#",""))<= 15 && Integer.parseInt(item.getDeviceCode().replace("#","")) <= 28).collect(Collectors.toList());
            result.add(one);
            result.add(two);
        }
        return result;
    }

    private ScanDeviceDto getScanDeviceDto(Device device) {
    ScanDeviceDto scanDeviceDto = new ScanDeviceDto();
    scanDeviceDto.setDeviceId(device.getDeviceId());
    scanDeviceDto.setDeviceCode(device.getDeviceNo());
    scanDeviceDto.setDeviceName(device.getDeviceName());
    scanDeviceDto.setProcessId(device.getProcessId());
    scanDeviceDto.setProcessName(device.getProcess());
    scanDeviceDto.setAbTag("");
    scanDeviceDto.setDeviceSignState("N");
    scanDeviceDto.setCurrentFlag("N");
    return scanDeviceDto;
}


@Autowired
private ObjectMapper json;

private Collection<ScanDeviceDto> parseDeviceIds(String machineryObj) {

    try {
        return json.<List<ScanDeviceDto>>readValue(machineryObj, new TypeReference<>() {
        });
    } catch (JsonProcessingException e) {
        log.error(e.getMessage());
        throw new BusinessException("解析机台异常");
    }
}

public static void main(String[] args) {
    String str="{\"appId\": \"whmskj.FZMES\", \"backendBundleEntry\": \".\", \"backendEpath\": \"roles/plantSystemsMaintainer/element.pkg\", \"backendResourcePath\": \"dist/roles/plantSystemsMaintainer//element.pkg\", \"buildTime\": 1757404511427, \"ePath\": \"roles/plantSystemsMaintainer/e.json\", \"extendType\": \"self\", \"frontBundleEntry\": \"./plantSystemsMaintainer.json\", \"frontResourcePath\": \"dist/roles/plantSystemsMaintainer/plantSystemsMaintainer.js\", \"fullName\": \"roles.plantSystemsMaintainer\", \"isCustom\": true, \"tag\": \"\\u5c09\\u7281\\u5229\\u534e\\u5e03\\u5382\", \"title\": \"\\u5e03\\u5382\\u7cfb\\u7edf\\u7ef4\\u62a4\\u5458\", \"type\": \"roles.NormalType\", \"backendResourceMD5\": \"6e67c19b31577935bfcdac29249ba6c2\", \"backendDistPath\": \"dist/roles/plantSystemsMaintainer//element.pkg\", \"frontResourceMD5\": \"c216a2cf104837940c223ca163186b11\", \"frontDistPath\": \"dist/roles/plantSystemsMaintainer/plantSystemsMaintainer.js\"}";
    JSONObject elementDefine = new JSONObject(str);
    String key = "tag";
    if(!elementDefine.containsKey(key))return;
    String[] split = elementDefine.getStr(key, "").split(",");
    System.out.println(Arrays.toString(split));
}

}